package com.surfilter.mq.validate.outernet;

import com.surfilter.mq.util.NetworkUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;


/**
 * 验证是否可以连接外网
 * @author 
 * @date   2016-5-3 14:36:43
 *
 */
public class OuterNetValidate {
	private static Logger logger = LoggerFactory.getLogger(OuterNetValidate.class);
	private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
	
	private static volatile boolean avaliable = true;
	
	private static List<String> urls = new ArrayList<String>();

	static{
		urls.add("www.baidu.com");
		urls.add("www.sina.cn");
		urls.add("www.taobao.com");
		urls.add("www.ifeng.com");
	}
	
	public void runOuterNetValidate(){
		 
		/*Runnable runnable=new Runnable() {			
			@Override
			
			public void run() {*/
				//	验证外网是否通的逻辑
				boolean  result=false;
				for(int i = 0;i < urls.size();i++){
					   if(result){
						   break;
					   }
					   String osName=System.getProperty("os.name"); //获得系统名称
					   if(osName.contains("Windows")){
						   result = NetworkUtils.pingWindows(urls.get(i), 4, 1);
					   }else{
						   result = NetworkUtils.pingLinux(urls.get(i), 4, 1);
					   }
				}
				avaliable = result;
				if(avaliable){
					logger.info("网络畅通");
				}else{
					logger.info("网络异常");
				}
			/*}
		};
		scheduler.scheduleAtFixedRate(runnable, 0, 2, TimeUnit.SECONDS);*/
	}


	public static boolean isAvaliable() {
		return avaliable;
	}


	public static void setAvaliable(boolean avaliable) {
		OuterNetValidate.avaliable = avaliable;
	}


	public static List<String> getUrls() {
		return urls;
	}


	public static void setUrls(List<String> urls) {
		OuterNetValidate.urls = urls;
	}
	
	
	
	
}
